{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "np.array([1, 2, 3, 4, 5])\n",
      " [1 2 3 4 5]\n",
      "np.array(range(5))\n",
      " [0 1 2 3 4]\n",
      "np.array(range(1, 6))\n",
      " [1 2 3 4 5]\n",
      "np.array(range(1, 10, 2))\n",
      " [1 3 5 7 9]\n"
     ]
    }
   ],
   "source": [
    "# 创建ndarray的各种方式\n",
    "import numpy as np\n",
    "\n",
    "# 使用列表创建ndarray\n",
    "a = np.array([1, 2, 3, 4, 5])\n",
    "print(\"np.array([1, 2, 3, 4, 5])\\n\", a)\n",
    "\n",
    "# 使用range创建ndarray\n",
    "a = np.array(range(5))\n",
    "print(\"np.array(range(5))\\n\", a)\n",
    "\n",
    "# 使用range创建ndarray\n",
    "a = np.array(range(1, 6))\n",
    "print(\"np.array(range(1, 6))\\n\", a)\n",
    "\n",
    "# 使用步长range创建ndarray加步长设置\n",
    "a = np.array(range(1, 10, 2))\n",
    "print(\"np.array(range(1, 10, 2))\\n\", a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 执行速度比较\n",
    "# 以随机数方式创建价格列表及数量列表\n",
    "import random\n",
    "import numpy as np\n",
    "\n",
    "# 生成价格列表\n",
    "pricelist = [round(random.uniform(10.0, 500.0), 2) for i in range(100)]     # list\n",
    "pricelist_np = np.array(pricelist)      # ndarray\n",
    "\n",
    "# 生成数量列表\n",
    "numlist = [random.randint(1, 10) for i in range(100)]       # list\n",
    "numlist_np = np.array(numlist)      # ndarray\n",
    "\n",
    "# 用于list计算总价\n",
    "def getTotal(plist, nlist):\n",
    "    total = 0\n",
    "\n",
    "    for i, j in zip(plist, nlist):\n",
    "        total += i * j\n",
    "        \n",
    "    return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "139163.55000000002"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getTotal(pricelist, numlist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.55 µs ± 111 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "timeit getTotal(pricelist, numlist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "139163.55"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(pricelist_np, numlist_np)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.28 µs ± 43.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
     ]
    }
   ],
   "source": [
    "timeit np.dot(pricelist_np, numlist_np)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d80dceb8e76061e928794bb3ce8fa9d0c2b7abe9c550ab61d3fca5a520d3bcb0"
  },
  "kernelspec": {
   "display_name": "Python 3.8.6 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
